package com.jeegit.ecms.utils;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.compress.utils.Lists;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jeegit.common.core.domain.TreeEntity;
import com.jeegit.common.core.domain.Ztree;
import com.jeegit.common.core.page.TableDataInfo;
import com.jeegit.common.utils.DictUtils;
import com.jeegit.common.utils.PageUtils;
import com.jeegit.common.utils.spring.SpringUtils;
import com.jeegit.common.utils.sql.SqlUtil;
import com.jeegit.ecms.domain.CmsArticle;
import com.jeegit.ecms.domain.CmsSite;
import com.jeegit.ecms.domain.CmsVideo;
import com.jeegit.ecms.domain.CmsVideoChapter;
import com.jeegit.ecms.domain.CmsVideoItems;
import com.jeegit.ecms.domain.CmsVideoTeacher;
import com.jeegit.ecms.mapper.CmsArticleMapper;
import com.jeegit.ecms.service.ICmsArticleCategoryService;
import com.jeegit.ecms.service.ICmsArticleService;
import com.jeegit.ecms.service.ICmsBannerCategoryService;
import com.jeegit.ecms.service.ICmsCollectionCategoryService;
import com.jeegit.ecms.service.ICmsLinkCategoryService;
import com.jeegit.ecms.service.ICmsSiteService;
import com.jeegit.ecms.service.ICmsVideoCategoryService;
import com.jeegit.ecms.service.ICmsVideoChapterService;
import com.jeegit.ecms.service.ICmsVideoItemsService;
import com.jeegit.ecms.service.ICmsVideoService;
import com.jeegit.ecms.service.ICmsVideoTeacherService;
import com.jeegit.ecms.service.impl.CmsArticleCategoryServiceImpl;
import com.jeegit.ecms.service.impl.CmsBannerCategoryServiceImpl;
import com.jeegit.ecms.service.impl.CmsCollectionCategoryServiceImpl;
import com.jeegit.ecms.service.impl.CmsLinkCategoryServiceImpl;
import com.jeegit.ecms.service.impl.CmsSiteServiceImpl;
import com.jeegit.ecms.service.impl.CmsVideoCategoryServiceImpl;
import com.jeegit.ecms.service.impl.CmsVideoChapterServiceImpl;
import com.jeegit.ecms.service.impl.CmsVideoServiceImpl;
import com.jeegit.ecms.service.impl.CmsVideoTeacherServiceImpl;
import com.jeegit.ecms.service.impl.CmsArticleServiceImpl;

import com.jeegit.system.domain.SysConfig;
import com.jeegit.system.service.ISysConfigService;
import com.jeegit.system.service.impl.SysConfigServiceImpl;
import com.jeegit.ecms.service.impl.CmsVideoItemsServiceImpl;

public class ECMSUtils {

    private static final class Static {
	private static ICmsVideoCategoryService CMS_VIDEO_CATEGORY_SERVICE = SpringUtils.getBean(CmsVideoCategoryServiceImpl.class);
	private static ICmsArticleCategoryService CMS_ARTICLE_CATEGORY_SERVICE = SpringUtils.getBean(CmsArticleCategoryServiceImpl.class);
	private static ICmsBannerCategoryService CMS_BANNER_CATEGORY_SERVICE = SpringUtils.getBean(CmsBannerCategoryServiceImpl.class);
	private static ICmsLinkCategoryService CMS_LINK_CATEGORY_SERVICE = SpringUtils.getBean(CmsLinkCategoryServiceImpl.class);
	private static ICmsCollectionCategoryService CMS_COLLECTION_CATEGORY_SERVICE = SpringUtils.getBean(CmsCollectionCategoryServiceImpl.class);
	private static ICmsVideoTeacherService CMS_VIDEO_TEACHER_SERVICE = SpringUtils.getBean(CmsVideoTeacherServiceImpl.class);
	private static ICmsVideoChapterService CMS_VIDEO_CHAPTER_SERVICE = SpringUtils.getBean(CmsVideoChapterServiceImpl.class);
	private static ICmsSiteService CMS_SITE_SERVICE = SpringUtils.getBean(CmsSiteServiceImpl.class);
	private static ISysConfigService CONFIG_SERVICE = SpringUtils.getBean(SysConfigServiceImpl.class);
	private static ICmsVideoService CMS_VIDEO_SERVICE = SpringUtils.getBean(CmsVideoServiceImpl.class);
	private static ICmsVideoItemsService CMS_VIDEO_ITEMS_SERVICE = SpringUtils.getBean(CmsVideoItemsServiceImpl.class);
	private static ICmsArticleService CMS_ARTICLE_SERVICE = SpringUtils.getBean(CmsArticleServiceImpl.class);
	private static CmsArticleMapper CMS_ARTICLE_DAO = SpringUtils.getBean(CmsArticleMapper.class);
	

    }

    public static Long getDefaultSiteId() {
	try {
	    return Long.parseLong(DictUtils.getDictValue("dict_default_site", "主站"));
	} catch (Exception e) {
	    return 1l;
	}
    }

    public static List<Ztree> getCategoryList(ECMSCategorys categorys) {
	switch (categorys) {
	case ARTICLE:
	    return Static.CMS_ARTICLE_CATEGORY_SERVICE.selectCmsArticleCategoryTree();
	case VIDEO:
	    return Static.CMS_VIDEO_CATEGORY_SERVICE.selectCmsVideoCategoryTree();
	case BANNER:
	    return Static.CMS_BANNER_CATEGORY_SERVICE.selectCmsBannerCategoryTree();
	case COLLECTION:
	    return Static.CMS_COLLECTION_CATEGORY_SERVICE.selectCmsCollectionCategoryTree();
	case LINK:
	    return Static.CMS_LINK_CATEGORY_SERVICE.selectCmsLinkCategoryTree();
	default:
	    break;
	}
	return null;
    }

    public static List<CmsVideoTeacher> getTeacherList(CmsVideoTeacher cmsVideoTeacher) {
	return Static.CMS_VIDEO_TEACHER_SERVICE.selectCmsVideoTeacherList(cmsVideoTeacher);
    }

    public static CmsVideoTeacher getTeacher(Long id) {
	return Static.CMS_VIDEO_TEACHER_SERVICE.selectCmsVideoTeacherById(id);
    }

    public static List<CmsVideoChapter> getCmsVideoChapterList(Long videoId) {
	CmsVideoChapter cmsVideoChapter = new CmsVideoChapter();
	cmsVideoChapter.setCmsVideoId(videoId);
	return Static.CMS_VIDEO_CHAPTER_SERVICE.selectCmsVideoChapterList(cmsVideoChapter);
    }

    
    public static CmsVideo getCmsVideo(Long videoId,Boolean isBuy) {
	
	CmsVideo cmsVideo=Static.CMS_VIDEO_SERVICE.selectCmsVideoById(videoId);
	
	if(cmsVideo==null) {
	    return null;
	}
	
	CmsVideoChapter  cmsVideoChapter=new CmsVideoChapter();
	cmsVideoChapter.setCmsVideoId(videoId);
	cmsVideo.setListCmsVideoChapter(Static.CMS_VIDEO_CHAPTER_SERVICE.selectCmsVideoChapterList(cmsVideoChapter));

	CmsVideoItems cmsVideoItems=new CmsVideoItems();
	cmsVideoItems.setCmsVideoId(videoId);
	List<CmsVideoItems> listCmsVideoItems=Static.CMS_VIDEO_ITEMS_SERVICE.selectCmsVideoItemsList(cmsVideoItems);
	
	for (int i = 0; i < listCmsVideoItems.size(); i++) {
	    for (int j = 0; j < cmsVideo.getListCmsVideoChapter().size(); j++) {
		if(listCmsVideoItems.get(i).getCmsVideoChapterId().equals(cmsVideo.getListCmsVideoChapter().get(j).getId())) {
		    if(!isBuy) {
			listCmsVideoItems.get(i).initIsBuy(cmsVideoItems,isBuy);
		    }
		    cmsVideo.getListCmsVideoChapter().get(j).getListCmsVideoItems().add(listCmsVideoItems.get(i));
		}
	    }
	}
	return cmsVideo;
    }

    
    
    public static CmsArticle getCmsArticle(Long videoId,Boolean isBuy) {
	
	CmsArticle cmsArticle=Static.CMS_ARTICLE_SERVICE.selectCmsArticleById(videoId);
	
	if(cmsArticle==null) {
	    return new CmsArticle();
	}
	
	CmsArticle updateCmsArticle=new CmsArticle();
	updateCmsArticle.setId(videoId);
	updateCmsArticle.setHits(cmsArticle.getHits()+1);
	Static.CMS_ARTICLE_DAO.updateCmsArticle(updateCmsArticle);
	return cmsArticle;
    }    
    
    
    
    
    
    
    
    public static Boolean getCmsVideoIsBuy(Long videoId) {
	return false;
    }
    public static Boolean getCmsArticleIsBuy(Long videoId) {
	return false;
    }
    
    public static List<CmsSite> getCmsSiteList() {
	return Static.CMS_SITE_SERVICE.selectCmsSiteList(new CmsSite());
    }

    public static List<Ztree> addTreeRoot(List<Ztree> ztrees) {
	Ztree ztreeRoot = Ztree.getInstance(0l, 0l, "根目录", "根目录");
	ztrees.add(ztreeRoot);
	return ztrees;
    }

    /**
     * 树结构值初始化
     * 
     * @param treeEntity
     */
    public static void initECMSTreeData(TreeEntity treeEntity) {
	if(treeEntity == null || treeEntity.getParentId() == null || treeEntity.getParentId() == 0l) {
	    treeEntity.setParentId(0l);
	    treeEntity.setParentName("根目录");
	}
    }

    public static String getSiteAccessCount() {
	return Static.CONFIG_SERVICE.selectConfigById(100l).getConfigValue() + "人";
    }

    public static SysConfig updateSiteAccessCount() {
	SysConfig sysConfig = Static.CONFIG_SERVICE.selectConfigById(100l);
	sysConfig.setConfigValue((Long.parseLong(sysConfig.getConfigValue()) + 1l) + "");
	Static.CONFIG_SERVICE.updateConfig(sysConfig);
	return sysConfig;
    }

    public static List<CmsVideo> getIndexVideo() {
	PageUtils.startPage(1, 8);
	return Static.CMS_VIDEO_SERVICE.selectCmsVideoList(new CmsVideo());
    }

    public static TableDataInfo getIndexVideoPage(int pageNum, int pageSize, CmsVideo cmsVideo) {
	PageUtils.startPage(pageNum, pageSize);
	String orderBy = "v.sort asc,v.update_time asc ";
        PageHelper.orderBy(orderBy);
	List<CmsVideo> list = Static.CMS_VIDEO_SERVICE.selectCmsVideoList(cmsVideo);
	return getDataTable(list);
    }
    
    
    
    public static List<CmsArticle> getIndexArticle() {
	PageUtils.startPage(1, 8);
	return Static.CMS_ARTICLE_SERVICE.selectCmsArticleList(new CmsArticle());
    }

    public static TableDataInfo getIndexArticlePage(int pageNum, int pageSize, CmsArticle cmsArticle) {
	PageUtils.startPage(pageNum, pageSize);
	String orderBy = "a.sort asc,a.update_time asc ";
        PageHelper.orderBy(orderBy);
	List<CmsArticle> list = Static.CMS_ARTICLE_SERVICE.selectCmsArticleList(cmsArticle);
	return getDataTable(list);
    }    
    
    
    
    
    

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static TableDataInfo getDataTable(List<?> list) {
	TableDataInfo rspData = new TableDataInfo();
	rspData.setCode(0);
	rspData.setRows(list);
	PageInfo pageInfo=new PageInfo(list);
	rspData.setTotal(pageInfo.getTotal());
	rspData.initPageInfo(pageInfo.getPageNum(),pageInfo.getPageSize(),pageInfo.getSize(),pageInfo.getStartRow(),pageInfo.getEndRow(),pageInfo.getPages(),pageInfo.getPrePage(),pageInfo.getNextPage(),pageInfo.isIsFirstPage(),
		pageInfo.isIsLastPage(),pageInfo.isHasPreviousPage(),pageInfo.isHasNextPage(),pageInfo.getNavigatePages(),pageInfo.getNavigatepageNums(),pageInfo.getNavigateFirstPage(),pageInfo.getNavigateLastPage());
	try {
	    System.out.println("rspData:" + JsonMapper.builder().build().writeValueAsString(rspData));
	} catch (JsonProcessingException e) {
	    e.printStackTrace();
	}
	return rspData;
    }
}
